package view;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

import model.Block;
import model.Bob;
import model.Bob.State;
import model.Bullet;
import model.World;

public class WorldRenderer {
	
	static final float CAMERA_WIDTH = 10f;
	static final float CAMERA_HEIGHT=7f;
	static final float RUNNING_FRAME_DURATION =0.06f;
	
	World world;
	OrthographicCamera cam;
	
	ShapeRenderer debugRenderer = new ShapeRenderer();
	
//textrueRegion
	
	//special item : Door
	TextureRegion doorTexture;
	
	
	//Bob frame
	/*TextureRegion bobIdleleft;
	TextureRegion bobIdleright;*/
	TextureRegion bobFrame;
	
	TextureRegion blockTexture;//texture of block
	//fire
	TextureRegion firesTexture;//texture of fire
	//TextureRegion firesCollis;
	//bob jump
	
	TextureRegion bobJumpLeft;
	TextureRegion bobFallLeft;
	TextureRegion bobJumpRight;
	TextureRegion bobFallRight;
	
	//sprite
	SpriteBatch spriteBatch;
	
	//animation of bob
	Animation walkleftAnimation;
	Animation walkrightAnimation;
	Animation idleLeftAnimation;
	Animation idleRightAnimation;
	//animation of fire
	Animation fireLeftAnimation;
	Animation fireRightAnimation;
	Animation fireCollis;
	//bien debug hinh
	boolean debug = false;
	int width;
	int height;
	float ppuX;//pixel per unit X
	float ppuY;//pixel per unit Y
	
	public void setSize(int w, int h){
		this.width = w;
		this.height = h;
		ppuX = (float)width/CAMERA_WIDTH;
		ppuY=(float)height/CAMERA_HEIGHT;
	}
	
	public WorldRenderer (World world, boolean debug){
		this.world = world;
		this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
		//this.cam.position.set(CAMERA_WIDTH/2f,CAMERA_HEIGHT/2f,0);		
		//cam.setToOrtho(true,CAMERA_WIDTH,CAMERA_WIDTH);
		this.cam.position.set(world.getBob().getPosition().x,world.getBob().getPosition().y,0);	
		this.debug= debug;
		spriteBatch = new SpriteBatch();
		loadTextures();
	}
	private void loadTextures(){
		TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));
		//bobIdleleft = atlas.findRegion("bob-01");
		/*bobIdleleft = atlas.findRegion("huy1");
		bobIdleright= new TextureRegion(bobIdleleft);
		bobIdleright.flip(true, false);*/
		blockTexture = atlas.findRegion("block1");
		
		bobJumpLeft = atlas.findRegion("huyjump");
		bobJumpRight = new TextureRegion(bobJumpLeft);
		bobJumpRight.flip(true, false);
		
		bobFallLeft=atlas.findRegion("huydown");
		bobFallRight= new TextureRegion(bobFallLeft);
		bobFallRight.flip(true,false);
		//load texture fire
		firesTexture = atlas.findRegion("fire");
		//firesCollis = atlas.findRegion("firebum");
		
		//animation ben trai
		TextureRegion[] walkLeftFrames= new TextureRegion[6];
		for(int i=0;i<6;i++)
			walkLeftFrames[i]= atlas.findRegion("huy"+(i+1));
		walkleftAnimation = new Animation (RUNNING_FRAME_DURATION,walkLeftFrames);
		
		//animation ben phai
		TextureRegion[] walkRightFrames= new TextureRegion[6];
		for(int i=0;i<6;i++){
			walkRightFrames[i]= new TextureRegion(walkLeftFrames[i]);
			walkRightFrames[i].flip(true, false);
		}
		walkrightAnimation = new Animation (RUNNING_FRAME_DURATION,walkRightFrames);
		//animation idle left
		TextureRegion[] idleLeftFrames = new TextureRegion[2];
		for(int i=0;i<2;i++){
			idleLeftFrames[i]= atlas.findRegion("huyidle"+(i+1));
		}
		idleLeftAnimation= new Animation(RUNNING_FRAME_DURATION+0.4f,idleLeftFrames);
		//animation idle right
		TextureRegion[] idleRightFrames= new TextureRegion[2];
		for(int i=0;i<2;i++){
			idleRightFrames[i] = new TextureRegion(idleLeftFrames[i]);
			idleRightFrames[i].flip(true, false);
		}
		idleRightAnimation=new Animation(RUNNING_FRAME_DURATION+0.4f,idleRightFrames);
		//animation of fireleft
		TextureRegion[] fireLeftFrames= new TextureRegion[2];
		for(int i=0;i<2;i++){
			fireLeftFrames[i]=atlas.findRegion("fire"+(i+1));
		}
		fireLeftAnimation = new Animation (RUNNING_FRAME_DURATION+2f,fireLeftFrames);
		//animation of fireright;
		TextureRegion[] fireRightFrames= new TextureRegion[2];
		for(int i=0;i<2;i++){
			fireRightFrames[i]=new TextureRegion(fireLeftFrames[i]);
			fireRightFrames[i].flip(true, false);
		}
		fireRightAnimation = new Animation (RUNNING_FRAME_DURATION+2f,fireRightFrames);
		//annimation of fire collistion
		TextureRegion[] fireCollisFrames= new TextureRegion[5];
		for(int i=0;i<5;i++){
			fireCollisFrames[i]=atlas.findRegion("firecollis"+(i+1));
		}
		fireCollis = new Animation (RUNNING_FRAME_DURATION, fireCollisFrames);
		//loat texture door
		doorTexture= atlas.findRegion("door");
	}
	public void render(){
		//moveCamera(world.getBob().getPosition().x,CAMERA_HEIGHT/2);
		moveCamera(world.getBob().getPosition().x,world.getBob().getPosition().y);
		spriteBatch.setProjectionMatrix(cam.combined);
		spriteBatch.begin();
			drawBlock();
			drawBob();
			drawFire();
			drawItemSpecial();
		spriteBatch.end();
		
		if(debug){
			drawCollistionBlocks();
			drawDebug();
		}
			
		
		
	}
	private void drawItemSpecial(){
		spriteBatch.draw(doorTexture,world.getPositionDoor().x,world.getPositionDoor().y, Block.SIZE,Block.SIZE);
	}
	private void drawCollistionBlocks(){
		debugRenderer.setProjectionMatrix(cam.combined);
		debugRenderer.begin(ShapeType.Filled);
		debugRenderer.setColor(new Color(1,1,1,1));
		for(Rectangle rec: world.getCollistionRects()){
			debugRenderer.rect(rec.x,rec.y,rec.width,rec.height);
			
		}
		debugRenderer.end();
		
		
	}
	private void drawFire(){
		for(Bullet fire : world.getFire()){
			if(fire.getState().equals(Bullet.State.FLY))
			firesTexture = world.getBob().isFacingLeft()?fireLeftAnimation.getKeyFrame(world.getBob().getStateTime(),true):fireRightAnimation.getKeyFrame(world.getBob().getStateTime(),true);
			else {
				firesTexture = fireCollis.getKeyFrame(world.getBob().getStateTime(),true);
				fire.nextCollisFrame();
			}
			spriteBatch.draw(firesTexture,fire.getPosition().x,fire.getPosition().y,fire.SIZE,fire.SIZE);
		}
	}
	private void drawBlock(){
		for(Block block: world.getDrawableBlocks((int) CAMERA_WIDTH,(int)CAMERA_HEIGHT)){
			spriteBatch.draw(blockTexture, block.getPosition().x, block.getPosition().y, Block.SIZE, Block.SIZE);
			
		}
	}
	private void drawBob(){
		Bob bob = world.getBob();
		bobFrame= bob.isFacingLeft()? idleLeftAnimation.getKeyFrame(bob.getStateTime(),true) : idleRightAnimation.getKeyFrame(bob.getStateTime(),true);
		if(bob.getState().equals(State.WALKING)){
			bobFrame =bob.isFacingLeft()? walkleftAnimation.getKeyFrame(bob.getStateTime(),true):walkrightAnimation.getKeyFrame(bob.getStateTime(),true);
		}
			  	 
		else if(bob.getState().equals(State.JUMPING)){
			  if(bob.getVelocity().y>0){
				  bobFrame =bob.isFacingLeft()?bobJumpLeft:bobJumpRight;
			  }
			  else if(bob.getVelocity().y<0)
			  {
				  bobFrame = bob.isFacingLeft()? bobFallLeft: bobFallRight;
			  }
		}
		else if(!bob.getGrounded())
			
			bobFrame = bob.isFacingLeft()? bobFallLeft: bobFallRight;		
		spriteBatch.draw(bobFrame,bob.getPosition().x,bob.getPosition().y,bob.SIZE,bob.SIZE);
							
	}
	public void setDebug(boolean debug){
		this.debug=debug;
	}
	public boolean isDebug(){
		return this.debug;
	}
	public void drawDebug(){
		//renders block
		debugRenderer.setProjectionMatrix(cam.combined);
		debugRenderer.begin(ShapeType.Line);
		for(Block block : world.getDrawableBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)){
			Rectangle rect = block.getBounds();
			float x1 = block.getPosition().x;//+rect.y;
			float y1 = block.getPosition().y;// + rect.y;
			debugRenderer.setColor(new Color(1,0,0,1));
			debugRenderer.rect(x1, y1, rect.width, rect.height);
		}
		//render bob
		Bob bob = world.getBob();
		Rectangle rect = bob.getBounds();
		float x1 = bob.getPosition().x;// + rect.y;
		float y1 = bob.getPosition().y;//+rect.y;
		debugRenderer.setColor(new Color(0,1,0,1));
		debugRenderer.rect(x1,y1,rect.width, rect.height);
		debugRenderer.end();
		
	}
	private void moveCamera(float x, float y){
		//if(world.getBob().getPosition().x>CAMERA_WIDTH/2f){
		cam.position.set(x,y,0);
		
		if(cam.position.x-CAMERA_WIDTH/2f<0)
			cam.position.x=CAMERA_WIDTH/2f;
		else if(cam.position.x+CAMERA_WIDTH/2>world.getLevel().getWidth())
			cam.position.x= world.getLevel().getWidth()-CAMERA_WIDTH/2;
		if(cam.position.y-CAMERA_HEIGHT/2f<0)
			cam.position.y=CAMERA_HEIGHT/2f;
		else if(cam.position.y+CAMERA_HEIGHT/2f>world.getLevel().getHeight())
			cam.position.y= world.getLevel().getHeight()-CAMERA_HEIGHT/2f;
		cam.update();
						
	//	}
		
	}
	
}
